package com.shushan.pages;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.Constants;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.home.adapter.AbsCommonAdapter;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetListCommListener;
import com.shushan.pages.bean.VkeBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.util.Session;
import com.shushan.util.ToolUtils;

import java.util.List;


/**
 * @ClassName: VkeListActivity
 * @Desciption: //TODO
 * @author: yichaohua
 * @date: 2018-12-10
 */
public class VkeListActivity extends NavigationLoseActivity implements AdapterView.OnItemClickListener {

    private static final String TAG = "VkeListActivity";

    private ListView listView;
    private AbsCommonAdapter vkeListAdapter;

    private List<VkeBean> vkeBeanList;

    private int subjectId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.v_ke_list_activity);

        setTitle("我的练习记录");

        subjectId = getIntent().getIntExtra("subjectId", Constants.SUBJECT_CHINESE_ID);

        initView();

        getData();
    }

    private void initView() {
        listView = findViewById(R.id.v_ke_list_lv);
        listView.setOnItemClickListener(this);
    }

    private void getData() {
        int child_id = BabyApplication.getInstance().getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.GET_V_KE_LIST);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("subject_id",subjectId)
                .putValue("child_id",child_id);
        showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetListCommListener<VkeBean>() {

                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(List<VkeBean> vkeBeans) {
                        vkeBeanList = vkeBeans;
                        if(!ToolUtils.isListEmpty(vkeBeans)){
                            vkeListAdapter = new AbsCommonAdapter<VkeBean>(VkeListActivity.this,R.layout.v_ke_list_item,vkeBeans) {
                                @Override
                                public void convert(ViewHolder helper, VkeBean item, int selectedItem) {
                                    helper.setText(R.id.v_ke_item_tv,item.video_title);
                                    if(helper.getPosition() == 0){
                                        helper.getView(R.id.v_ke_item_img1).setVisibility(View.GONE);
                                    }else if(vkeBeanList != null && (helper.getPosition() == (vkeBeanList.size()-1))){
                                        helper.getView(R.id.v_ke_item_img2).setVisibility(View.GONE);
                                    }
                                }
                            };
                            listView.setAdapter(vkeListAdapter);
                        }

                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        VkeBean vkeBean = vkeBeanList.get(position);

        Session.getSession().put("vkeBean",vkeBean);
        Intent intent = new Intent(this,VkeDetailActivity.class);
        intent.putExtra("subjectId",subjectId);
        startActivityAllow(intent);
    }
}
